//
//  ViewController.m
//  UITableView 16.3.4
//
//  Created by zhangxin on 16/3/4.
//  Copyright © 2016年 zhangxin. All rights reserved.
//

#import "ViewController.h"
#import "czherogroup.h"
//遵循数据源协议
@interface ViewController ()<UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

//数据模型的建立
@property(nonatomic,strong)NSArray * heroGroups;

@end

@implementation ViewController

//懒加载 － 延迟加载
//重写get方法
- (NSArray * )heroGroups
{
    //return _heroGroups;
    if (_heroGroups == nil) {
        //读取plist文件
        //1.全路径
        NSString * plistPath = [[NSBundle mainBundle]pathForResource:@"heroPlist.plist" ofType:nil];
        //2.根据路径获取plist文件的数据
        //需要将获取的数据保存到某个对象中，该对象类型和plist文件的根节点的类型一致。
        NSArray * heros = [[NSArray alloc]initWithContentsOfFile:plistPath];
        //NSLog(@"%@",heros);
        //NSArray * heros2 = [NSArray arrayWithContentsOfFile:plistPath];
        //3.字典转模型
        //3.1面向模型开发，建立一个模型对象
        //3.2讲plist文件中key之作为模型的属性
        
        NSMutableArray * tempArray = [NSMutableArray array];
        for (NSDictionary * dict in heros) {
            czherogroup * HeroGroup = [czherogroup heroGroupWithDict:dict];
            [tempArray addObject:HeroGroup];
        }
        _heroGroups = tempArray;
        
        }
    return _heroGroups;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //设置数据源
    self.tableView.dataSource = self;
   
}


//协议中方法的实现
//1、返回tableview的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //设置tableview有3组
    //return 3;
    return self.heroGroups.count;
}

//2.返回行数（每一组的行数）
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return 3;
   /* if (section == 0) {
        return 1;
    }
    else if (section == 1)
    {
        return 2;
    }else
    {
        return 3;
    }*/
    czherogroup * heroGroup = self.heroGroups[section];
    return heroGroup.heros.count;
}


//3.将数组添加到每一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //UITableViewCell:每一行的格子
    //indexPath:代表唯一的行
    //indexPath 有两个属性：section row
    //indexPath.section : 获取到组号
    //indexPath.row : 获取到行号
    
    //创建cell
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    //设置数据到cell
    czherogroup * heroGroup = self.heroGroups[indexPath.section];
    cell.textLabel.text  = heroGroup.heros[indexPath.row];
    NSLog(@"count=%lu",heroGroup.heros.count);
    
    /*if (indexPath.section == 0 ) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"zx";
//(每行添加不一样的图片)
            cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
        }
        
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"xin";
//(每行添加不一样的图片)
            cell.imageView.image = [UIImage imageNamed:@"5.jpg"];


        }else{
            cell.textLabel.text = @"xiao";
//(每行添加不一样的图片)
            cell.imageView.image = [UIImage imageNamed:@"6.jpg"];
        }
    }
        else
        {
            if(indexPath.row == 0){
           
                cell.textLabel.text = @"yin";
//(每行添加不一样的图片)
                cell.imageView.image = [UIImage imageNamed:@"7.jpg"];

            }else if(indexPath.row == 1){
                cell.textLabel.text = @"yao";
//(每行添加不一样的图片)
                cell.imageView.image = [UIImage imageNamed:@"8.jpg"];
            }
            else{
                cell.textLabel.text = @"yu";
//(每行添加不一样的图片)
                cell.imageView.image = [UIImage imageNamed:@"9.jpg"];
            }
        }
    
//(每行添加一样的图片)
                cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
     */
    
    return cell;
    
}

/*- (NSString * )tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0 ) {
        return @"Z";
    }else if (section == 1) {
        return @"x";
    }else{
        return @" Y";
    }
    
}

- (NSString * )tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    if (section == 0 ) {
        return @"大学";
    }else if (section == 1) {
        return @"高中";
    }else{
        return @"初中";
    }
    
    
}*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
